﻿package kliment.utils {
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.0.5
	 * 
	 * class name: kliment.utils.IntervalTimer
	 */
	public final class IntervalTimer {
		private var _intervalID:uint;
		private var _loop:Boolean
		private var _closure:Function;
		private var _delay:Number;
		private var _argumsArray:Array;
		
		public function IntervalTimer(closure:Function, delay:Number, loop:Boolean = false, autostart:Boolean = true, argArray:Array = null) {
			_closure = closure;
			_delay = delay;
			_loop = loop;
			_argumsArray = argArray;
			if (autostart)
				start();
		}
		
		public function start(argum:Array = null):void {
			stop();
			if (argum)
				_argumsArray = argum;
			_intervalID = setInterval(_intervalHandler, _delay);
		}
		
		public function stop():void {
			clearInterval(_intervalID);
		}
		
		private function _intervalHandler():void {
			if (!_loop)
				stop();
			_closure.apply(null, _argumsArray);
		}
		
		public function get delay():Number { return _delay; }
		public function set delay(value:Number):void {
			_delay = value;
		}
	}

}